﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading;
using Kugar.Core.BaseStruct;
using Kugar.Core.Collections;

namespace Kugar.Core.WinForm.ControlFlash
{
    public class ControlFlashManage
    {
        private Timer timer = null;

        private ListEx<FlashItemBase> itemList = new ListEx<FlashItemBase>();

        private static ControlFlashManage instans = null;

        private ControlFlashManage(int flashDueTime)
        {
            FlashDueTime = flashDueTime;

            if (flashDueTime < 500)
            {
                throw new Exception("闪烁的时间间隔不能少于500毫秒");
            }

            itemList.CollectionChanging += itemList_CollectionChanging;

            timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
        }

        //当添加或者赋值新的闪烁Item的时候，如果出现相同的，则不添加
        void itemList_CollectionChanging(object sender, NotifyCollectionChangingEventArgs<FlashItemBase> e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Replace:
                    FlashItemBase tempItem = null;
                    for (int i = 0; i < itemList.Count; i++)
                    {
                        var item = itemList[i];

                        if (Equals(item, e.NewItems[0]))
                        {
                            tempItem = e.NewItems[0];
                        }
                    }

                    if (tempItem!=null)
                    {
                        this.itemList.Remove(tempItem);
                        tempItem.Stop();                        
                    }


                    break;
            }
        }

        /// <summary>
        ///     关闭当前闪烁控制类
        /// </summary>
        public void Close()
        {
            Stop();

            itemList.Clear();
        }

        /// <summary>
        ///     停止所有的闪烁控件，并还原所有闪烁控件的原色
        /// </summary>
        public void Stop()
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);

            lock (itemList)
            {
                foreach (var item in itemList)
                {
                    item.SourceControl.Invoke(new SetItemColor(item.Stop));
                }
            }
        }

        /// <summary>
        ///     开始启用闪烁控件，并将所有选择的控件都改为允许闪烁
        /// </summary>
        public void Start()
        {
            lock (itemList)
            {
                foreach (var item in itemList)
                {
                    item.SourceControl.Invoke(new SetItemColor(item.Flash));
                }
            }

            timer.Change(FlashDueTime * 2, 200);
        }

        /// <summary>
        ///     控件闪烁的间隔时间
        /// </summary>
        public int FlashDueTime { private set; get; }

        public ListEx<FlashItemBase> ItemList { get { return itemList; } }

        public static ControlFlashManage Create()
        {
            if (instans == null)
            {
                instans = new ControlFlashManage(600);
            }

            return instans;

        }

        private void OnTimer(object state)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);

            lock (itemList)
            {
                for (int i = 0; i < itemList.Count; i++)
                {
                    var item = itemList[i];

                    switch (item.Status)
                    {
                        case FlashType.Static:
                        case FlashType.Disable:
                            continue;
                        case FlashType.Dynamic:
                            item.Flash();
                            break;
                    }
                }
            }


            //重新打开计时器
            timer.Change(this.FlashDueTime, 10);

        }

        private delegate void SetItemColor();
    }
}
